<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <a-row>
        <a-col :span="6">
          <a-button v-action:add type="primary" icon="plus" @click="handleAdd">新增</a-button>
        </a-col>
        <!-- TODO: 后面有时间补上模板搜索功能 -->
        <!-- <a-col :span="8" :offset="10">
          <a-input-search
            v-action:search
            style="max-width: 300px; min-width: 150px; float: right"
            v-model="queryParam.search"
            placeholder="请输入模板名字"
            @search="onSearch"
          />
        </a-col> -->
      </a-row>
    </div>
    <s-table ref="table" rowKey="time_template_id" :loading="isLoading" :columns="columns" :data="loadData" :pageSize="15">
      <div slot="name" slot-scope="text, item">
        <span class="template_name" @click="toTimeList(item)">{{ text }}</span>
      </div>
      <div class="actions" slot="action" slot-scope="text, item">
        <a v-action:edit @click="handleEdit(item)">编辑</a>
        <template>
          <a v-action:delete @click="handleDelete(item)">删除</a>
        </template>

        <a  @click="toTimeList(item)">配置</a>
      </div>
    </s-table>
    <AddForm ref="AddForm" @handleSubmit="handleRefresh" />
    <EditForm ref="EditForm" @handleSubmit="handleRefresh" />
  </a-card>
</template>

<script>
import * as Api from '@/api/time/template'
import { STable } from '@/components'
import AddForm from './time/AddForm.vue'
import EditForm from './time/EditForm.vue'

export default {
  name: 'TimeTemplate',
  components: {
    STable,
    AddForm,
    EditForm
  },
  data() {
    return {
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      // 表头
      columns: [
        {
          title: '模板ID',
          dataIndex: 'time_template_id',
        },
        {
          title: '模板名称',
          dataIndex: 'name',
          scopedSlots: { customRender: 'name' },
        },
        {
          title: '排序',
          dataIndex: 'sort',
          align: 'center'
        },
        {
          title: '添加时间',
          dataIndex: 'create_time',
          width: '220px',
          align: 'center'
        },
        {
          title: '更新时间',
          dataIndex: 'update_time',
          width: '220px',
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' },
          align: 'center'
        },
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: (param) => {
        return Api.list({ ...param, ...this.queryParam })
          .then(response => {
            return response.data.list
          })
      }
    }
  },
  created() {},
  methods: {
    /**
     * 新增记录
     */
    handleAdd () {
      this.$refs.AddForm.add()
    },

    /**
     * 编辑记录
     */
    handleEdit (item) {
      this.$refs.EditForm.edit(item)
    },

    /**
     * 删除记录
     */
    handleDelete(item) {
      const app = this
      const modal = this.$confirm({
        title: '您确定要删除该记录吗?',
        content: '删除后不可恢复',
        onOk() {
          return Api.del({ timeTemplateId: item['time_template_id'] })
            .then((result) => {
              app.$message.success(result.message, 1.5)
              app.handleRefresh()
            })
            .finally((result) => {
              modal.destroy()
            })
        },
      })
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh(bool = false) {
      this.$refs.table.refresh(bool)
    },

    /**
     * 检索查询
     */
    onSearch() {
      this.handleRefresh(true)
    },

    // 去对应时间段设置页面
    toTimeList(item) {
      this.$router.push({
        name: 'timeFragment',
        query: {
          templateId: item.time_template_id,
          templateName: item.name
        }
      })
    }
  },
}
</script>

<style lang="less" scoped>
.template_name{
  cursor: pointer;
  color: rgba(0,160,233,.7);
  font-weight: 700;
}
</style>